package org.mspring.mlog.api.ping;

import java.io.IOException;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
import org.mspring.platform.utils.XMLUtils;
import org.w3c.dom.Document;

/**
 * 
 * @author Gao Youbo
 * @since 2013年7月25日
 */
public class GooglePing implements IPing {
    public static final String PING_URL = "http://blogsearch.google.com/ping/RPC2";

    @Override
    public boolean ping(String name, String indexUrl, String postUrl, String rssUrl) throws ClientProtocolException, IOException {
        // TODO Auto-generated method stub
        StringBuffer requestXML = new StringBuffer();
        requestXML.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
        requestXML.append("<methodCall>");
        requestXML.append(" <methodName>weblogUpdates.extendedPing</methodName>");
        requestXML.append(" <params>");
        requestXML.append("     <param><value><string>" + name + "</string></value></param>");
        requestXML.append("     <param><value><string>" + indexUrl + "</string></value></param>");
        requestXML.append("     <param><value><string>" + postUrl + "</string></value></param>");
        requestXML.append("     <param><value><string>" + rssUrl + "</string></value></param>");
        requestXML.append(" </params>");
        requestXML.append("</methodCall>");

        HttpClient httpClient = new DefaultHttpClient();

        HttpEntity entity = new StringEntity(requestXML.toString(), "text/xml", "UTF-8");

        HttpPost httpPost = new HttpPost(PING_URL);
        httpPost.addHeader("Content-Type", "text/xml");
        httpPost.setEntity(entity);

        HttpResponse httpResponse = httpClient.execute(httpPost);
        int status = httpResponse.getStatusLine().getStatusCode();
        if (status != 200) {
            return false;
        }
        String responseXML = EntityUtils.toString(httpResponse.getEntity());
        Document doc = XMLUtils.parse(responseXML);
        try {
            String result = XMLUtils.parseForString(doc, "/methodResponse/params/param/value/struct/member[1]/value/boolean");
            // String message = XMLUtils.parseForString(doc,
            // "/methodResponse/params/param/value/struct/member[2]/value");
            return "0".equals(result) || "true".equalsIgnoreCase(result);
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            return false;
        }
    }

    public static void main(String[] args) throws ClientProtocolException, IOException {
        IPing ping = new GooglePing();
        boolean result = ping.ping("百度的空间", "http://hi.baidu.com/baidu/", "http://baidu.com/blog/example.html", "http://hi.baidu.com/baidu/rss");
        System.out.println(result);
    }

}
